package com.nike.amoeba.bean.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.nike.amoeba.bean.db.StockManageEpcBean

/**
 * @Author nike
 * @Date 2023/7/28 10:34
 * @Description
 */
@Dao
interface StockManageEpcDao {

    /**
     * 增加
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun add(vararg bean: StockManageEpcBean)

    /**
     * 查一个
     */
    @Query("SELECT * FROM StockManageEpcBean WHERE EPCRoNo =:EPCRoNo")
    fun find(EPCRoNo: String): StockManageEpcBean

    /**
     * 根据库存查询
     */
    @Query("SELECT * FROM StockManageEpcBean WHERE id =:id AND MURoNo =:mURoNo AND type = 1")
    fun findMURoNoAll(id: String, mURoNo: String): List<StockManageEpcBean>

    /**
     * 根据库存查询
     */
    @Query("SELECT * FROM StockManageEpcBean WHERE id =:id AND type = 1")
    fun findMURoNoAll(id: String): List<StockManageEpcBean>

    /**
     * 查找库存有多少符合
     */
    @Query("SELECT * FROM StockManageEpcBean WHERE ids =:ids AND BatchNo =:batchNo AND type = 1")
    fun findSkuAll(ids: String, batchNo: String): List<StockManageEpcBean>
    @Query("SELECT * FROM StockManageEpcBean WHERE ids =:ids AND MURoNo =:mURoNo AND BatchNo =:batchNo AND type = 1")
    fun findSkuAll(ids: String, mURoNo: String, batchNo: String): List<StockManageEpcBean>
    @Query("SELECT * FROM StockManageEpcBean WHERE ids =:ids AND EPCRoNo =:epcRoNo AND BatchNo =:batchNo")
    fun findSkuId(ids: String, epcRoNo: String, batchNo: String): StockManageEpcBean

    /**
     * 更新
     */
    @Update
    suspend fun update(bean: StockManageEpcBean)

    //删除数据库表全部数据
    @Query("delete from StockManageEpcBean")
    suspend fun deleteAll()

    //根据ID删除全部
    @Query("DELETE FROM StockManageEpcBean WHERE id = :id")
    suspend fun deleteIdAll(id: String)

    suspend fun insertOrUpdate(bean: StockManageEpcBean) {
        val find = find(bean.EPCRoNo!!)
        if (find == null){
            add(bean)
        }else{
            update(bean)
        }
    }

}